package com.chinamsp.dydb.dbsource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

/**
 * 数据源管理
 * @author shifenghu
 *
 */
@Component
public class DataSourceManager {
	
	/**
	 * 主库实例列表
	 */
	private Map<String, DataSourceImpl> masters;
	
	/**
	 * 从库实例列表
	 */
	private Map<String, List<DataSourceImpl>> slaves;
	
	/**
	 * 默认数据源名称
	 */
	private DataSourceImpl defaultSource;
	
	/**
	 * 默认数据源名称
	 */
	private String defaultSourceName;
	
	public DataSourceManager() {
		this.masters = new HashMap<String, DataSourceImpl>(8);
		this.slaves = new HashMap<String, List<DataSourceImpl>>(8);
	}

	public Map<String, DataSourceImpl> getMasters() {
		return masters;
	}

	public void setMasters(Map<String, DataSourceImpl> masters) {
		this.masters = masters;
	}

	public Map<String, List<DataSourceImpl>> getSlaves() {
		return slaves;
	}

	public void setSlaves(Map<String, List<DataSourceImpl>> slaves) {
		this.slaves = slaves;
	}
	
	/**
	 * 添加一个 从库
	 * @param module
	 * @param impl
	 */
	public void putSalve(String module, DataSourceImpl impl) {
		List<DataSourceImpl> list = slaves.get(module);
		if (list == null) {
			list = new ArrayList<DataSourceImpl>(8);
			slaves.put(module, list);
		}
		list.add(impl);
	}

	public DataSourceImpl getDefaultSource() {
		return defaultSource;
	}

	public void setDefaultSource(DataSourceImpl defaultSource) {
		this.defaultSource = defaultSource;
	}

	public String getDefaultSourceName() {
		return defaultSourceName;
	}

	public void setDefaultSourceName(String defaultSourceName) {
		this.defaultSourceName = defaultSourceName;
	}



	/**
	 * 主库 后缀
	 */
	public static final String MASTER_SUFFIX = "-master";
	
	/**
	 * 从库后缀
	 */
	public static final String SLAVE_SUFFIX = "-slave";
	
	
}
